package com.cmw.auntapp.ui;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.cmw.auntapp.R;
import com.cmw.auntapp.adapter.OrderListAdapter;
import com.cmw.auntapp.constant.CommandType;
import com.cmw.auntapp.core.base.vo.DataResult;
import com.cmw.auntapp.core.util.FormUtil;
import com.cmw.auntapp.core.util.StringHandler;
import com.cmw.auntapp.listener.BackListener;
import com.cmw.auntapp.model.OrderModel;
import com.cmw.auntapp.presenter.OrderPresenter;
import com.fantasy.doubledatepicker.DoubleDateSelectDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cmw.auntapp.R2.id.tv_address_val;
import static com.cmw.auntapp.R2.id.tv_cname_val;
import static com.cmw.auntapp.R2.id.tv_code_val;
import static com.cmw.auntapp.R2.id.tv_order_xstatus;
import static com.cmw.auntapp.R2.id.tv_tel_val;
import static com.cmw.auntapp.R2.id.tv_val_comment;
import static com.cmw.auntapp.R2.id.tv_val_create_time;
import static com.cmw.auntapp.R2.id.tv_val_cust_name;
import static com.cmw.auntapp.R2.id.tv_val_end_time;
import static com.cmw.auntapp.R2.id.tv_val_ocode;
import static com.cmw.auntapp.R2.id.tv_val_price;
import static com.cmw.auntapp.R2.id.tv_val_receivable;
import static com.cmw.auntapp.R2.id.tv_val_receive_time;
import static com.cmw.auntapp.R2.id.tv_val_reward_amount;
import static com.cmw.auntapp.R2.id.tv_val_service_address;
import static com.cmw.auntapp.R2.id.tv_val_service_time;
import static com.cmw.auntapp.R2.id.tv_val_start_time;
import static com.cmw.auntapp.R2.id.tv_val_total_amount;
import static com.cmw.auntapp.constant.Constant.ORDER_XSTATUS_DES;

/**
 * 用户中心--> 订单详情
 * @Auth chengmingwei
 * @date 2021-04-22 07:45
 */
public class OrderDetailActivity extends BaseActivity {

    private OrderPresenter orderPresenter;

    private JSONObject orderData; //订单数据
    @Override
    protected int getActivityLayout() {
        return R.layout.activity_order_detail;
    }


    @Override
    void onInit(Bundle savedInstanceState) {
        setTitle(R.string.order_list_title);
        orderPresenter = new OrderPresenter(this);
    }

    @Override
    protected BackListener getCustBackListener() {
        return (view) -> go(OrderListActivity.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume...");
        loadData();
    }

    private void loadData() {
        String orderId = getOrderId();
        orderPresenter.get(orderId);
    }

    private String getOrderId() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        return bundle.getString("orderId");
    }


    @Override
    protected boolean doFailure(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case GET:{ //订单详情数据

                break;
            } case PUT:{ // 在/离岗切换失败
                break;
            }
        }
        return true;
    }

    @Override
    protected boolean doSuccess(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case GET:{ //订单详情数据
                orderData = (JSONObject)dataResult.getDatas();
                setValues();
                break;
            }
        }
        return true;
    }


    /**
     * 为页面元素赋值
     */
    private void setValues() {
        int[] viewIds = new int[]{ tv_order_xstatus,tv_val_cust_name,tv_val_ocode,tv_val_create_time,tv_val_receive_time,
                tv_val_service_address,
                tv_val_start_time,tv_val_end_time,tv_val_service_time,tv_val_price,tv_val_reward_amount,tv_val_total_amount,
                tv_val_comment,tv_val_receivable};
        Integer xstatus = orderData.getInteger("xstatus");
        String serviceTime = orderData.getString("serviceTime");
        String price = orderData.getString("price");
        String discountAmount = orderData.getString("discountAmount");
        String totalAmount = orderData.getString("totalAmount");
        String payAmount = orderData.getString("payAmount");
        String xstatusName = ORDER_XSTATUS_DES.get(xstatus);

        orderData.put("xstatus", xstatusName);
        orderData.put("serviceTime", StringHandler.getVal(serviceTime, "0")+"分钟");
        orderData.put("price", StringHandler.getVal(price, "0")+"元");
        orderData.put("discountAmount", StringHandler.getVal(discountAmount, "0")+"元");
        orderData.put("totalAmount", StringHandler.getVal(totalAmount, "0")+"元");
        orderData.put("payAmount", StringHandler.getVal(payAmount, "0")+"元");

        FormUtil.setValues(this, orderData, viewIds);
        Integer starType = orderData.getInteger("starType");
        if (starType != null && starType.intValue() > 0) {
            int[] starViewIds = {R.id.comment_star1,R.id.comment_star2,
                    R.id.comment_star3,R.id.comment_star4};
            for(int i=0; i < starType; i++){
                int starViewId = starViewIds[i];
                ImageView imageView = findViewById(starViewId);
                imageView.setImageResource(R.drawable.icon_star);
            }
        }
    }
}